<div class="page">
	<?php echo $this->Form->create('Emprestimo', array('class' => 'form-horizontal'));?>
	<div class="page-header">
		<div class="pull-left">
			<i class="icon-awesome-file-alt icon-large"></i>
			<span class="divider"></span>
			<div class="title">
				<h3><?php echo __('Novo Empréstimo'); ?></h3>
				<small><?php echo ('Informe os cados a seguir para criação de um novo empréstimo'); ?></small>
			</div>
		</div>
		<div class="pull-right text-right">
			<?php echo $this->Form->button('<i class="icon-awesome-save icon-white"></i>' . __('Salvar'), array('type' => 'submit', 'name' => 'data[Emprestimo][submit]', 'value' => (empty($this->request->data['Emprestimo']['id'])) ? 'create' : 'save', 'escape' => false, 'class' => 'btn btn-primary btn-square')); ?>
			<?php echo $this->Html->link('<i class="icon-awesome-undo icon-white"></i>' . __('Voltar'), array('action' => 'index'), array('escape' => false, 'class' => 'btn btn-square')); ?>
		</div>
	</div>
	<div class="page-content">
		<p class="required">Os campos com <em>*</em> são de preenchimento obrigatório.</p>
		<fieldset>
			<legend><?php echo __('Dados do Empréstimo'); ?></legend>
			<div class="row-fluid">
				<div class="control-group required span9 <?php echo ($this->Form->isFieldError('aluno_id')) ? 'error' : ''; ?>">
					<?php echo $this->Form->Label('aluno_id', 'Solicitante:', array('class' => 'control-label'));	?>
					<div class="controls">
						<div class="input-append span11">
							<?php echo $this->Form->input('aluno_id', array('empty' => true, 'label' => false, 'div' => false, 'class' => 'span12', 'required' => false, 'error' => false, 'placeholder' => 'Nome do Solicitante'));	?>
							<?php echo $this->Html->link('<i class="icon-plus icon-white"></i>', array('controller' => 'alunos', 'action' => 'add'), array('class' => 'btn btn-primary', 'escape' => false, 'id' => 'btn-add-solicitante', 'title' => 'Cadastrar Novo Aluno')); ?>
						</div>
						<div class="clearfix"></div>
						<?php echo $this->Form->error('aluno_id', null, array('wrap' => 'span', 'class' => 'clearfix help-block')); ?>
					</div>
				</div>
			</div>
			<div class="row-fluid">
				<div class="control-group required span9 <?php echo ($this->Form->isFieldError('projeto_id')) ? 'error' : ''; ?>">
					<?php echo $this->Form->Label('projeto_id', 'Projeto:', array('class' => 'control-label'));	?>
					<div class="controls">
						<div class="input-append span11">
							<?php echo $this->Form->input('projeto_id', array('empty' => true, 'label' => false, 'div' => false, 'class' => 'span12', 'required' => false, 'error' => false, 'placeholder' => 'Projeto do Solicitante'));	?>
							<?php echo $this->Html->link('<i class="icon-plus icon-white"></i>', array('controller' => 'projetos', 'action' => 'add'), array('class' => 'btn btn-primary', 'escape' => false, 'id' => 'btn-add-projeto', 'title' => 'Cadastrar Novo Projeto')); ?>
						</div>
						<div class="clearfix"></div>
						<?php echo $this->Form->error('projeto_id', null, array('wrap' => 'span', 'class' => 'help-block')); ?>
					</div>
				</div>
			</div>
			<div class="row-fluid">
				<div class="control-group required span9 <?php echo ($this->Form->isFieldError('autorizador_id')) ? 'error' : ''; ?>">
					<?php echo $this->Form->Label('autorizador_id', 'Professor:', array('class' => 'control-label'));	?>
					<div class="controls">
						<?php echo $this->Form->input('autorizador_id', array('empty' => true, 'label' => false, 'div' => false, 'class' => 'span11', 'required' => false, 'error' => false, 'placeholder' => 'Professor que autorizou'));	?>
						<div class="clearfix"></div>
						<?php echo $this->Form->error('autorizador_id', null, array('wrap' => 'span', 'class' => 'help-block')); ?>
					</div>
				</div>
			</div>
			<div class="row-fluid">
				<div class="control-group required span10 <?php echo ($this->Form->isFieldError('data_inicio')) ? 'error' : ''; ?>">
					<?php echo $this->Form->Label('data_inicio', 'Período:', array('class' => 'control-label'));	?>
					<div class="controls controls-row">
						<div class="input-append span5">
							<?php $this->request->data['Emprestimo']['data_inicio'] = (!empty($this->request->data['Emprestimo']['data_inicio'])) ? date('d/m/Y', strtotime($this->request->data['Emprestimo']['data_inicio'])) : ''; ?>
							<?php echo $this->Form->input('data_inicio', array('type' => 'text', 'label' => false, 'div' => false, 'class' => 'span10', 'placeholder' => 'Data do Início', 'required' => false, 'error' => false)); ?>
							<span class="add-on"><i class="icon-calendar"></i></span>
						</div>
						<div class="input-append span5">
							<?php $this->request->data['Emprestimo']['data_entrega'] = (!empty($this->request->data['Emprestimo']['data_entrega'])) ? date('d/m/Y', strtotime($this->request->data['Emprestimo']['data_entrega'])) : ''; ?>	
							<?php echo $this->Form->input('data_entrega', array('type' => 'text', 'label' => false, 'div' => false, 'class' => 'span10', 'placeholder' => 'Data da Entrega', 'required' => false, 'error' => false)); ?>
							<span class="add-on"><i class="icon-calendar"></i></span>
						</div>
						<div class="clearfix"></div>
						<?php echo $this->Form->error('data_inicio', null, array('wrap' => 'span', 'class' => 'help-block')); ?>
					</div>
				</div>
			</div>
		</fieldset>
	</div>
	<?php echo $this->Form->end(); ?>
</div>

<div id="modal-add-solicitante" class="modal hide fade">
	<div class="modal-header">
		<?php echo $this->Form->button('×', array('type' => 'button', 'div' => false, 'class' => 'close', 'data-dismiss' => 'modal')); ?>
		<h4>Cadastrar Novo Solicitante</h4>
	</div>
	<div class="modal-body">
	</div>
	<div class="modal-footer">
		<?php echo $this->Form->button('<i class="icon-awesome-remove icon-white"></i>&nbsp;<span>' . __('Cancelar') . '</span>', array('div' => false, 'class' => 'btn', 'data-dismiss' => 'modal'));?>		
		<?php echo $this->Form->button('<i class="icon-awesome-save icon-white"></i>&nbsp;<span>' . __('Salvar') . '</span>', array('div' => false, 'class' => 'btn btn-primary', 'id' => 'btn-save-solicitante')); 	?>
	</div>
</div>

<div id="modal-add-projeto" class="modal hide fade">
	<div class="modal-header">
		<?php echo $this->Form->button('×', array('type' => 'button', 'div' => false, 'class' => 'close', 'data-dismiss' => 'modal')); ?>
		<h4>Criar Novo Projeto</h4>
	</div>
	<div class="modal-body">
	</div>
	<div class="modal-footer">
		<?php echo $this->Form->button('<i class="icon-awesome-remove icon-white"></i>&nbsp;<span>' . __('Cancelar') . '</span>', array('div' => false, 'class' => 'btn', 'data-dismiss' => 'modal'));?>		
		<?php echo $this->Form->button('<i class="icon-awesome-save icon-white"></i>&nbsp;<span>' . __('Salvar') . '</span>', array('div' => false, 'class' => 'btn btn-primary', 'id' => 'btn-save-projeto')); 	?>
	</div>
</div>

<script type="text/javascript">
	$(document).ready(function () {
		$('a, button').tooltip();
		$('#EmprestimoAlunoId').select2({placeholder: 'Selecione um solicitante...', allowClear: true, formatNoMatches: function(term) { return 'Nenhum aluno encontrado'; }});
		$('#EmprestimoProjetoId').select2({placeholder: 'Selecione um projeto...', allowClear: true});
		$('#EmprestimoAutorizadorId').select2({placeholder: 'Selecione um professor...', allowClear: true});
		$('#EmprestimoDataInicio').mask('99/99/9999').datepicker();
		$('#EmprestimoDataEntrega').mask('99/99/9999').datepicker();
		$('#Itensemprestimo0EquipamentoId').select2({placeholder: 'Pesquise um equipamento...', allowClear: true});

		$('#btn-add-solicitante').click(function(event){
			event.preventDefault();
			$("#ajax-indicator").show();
			$("#modal-add-solicitante .modal-body").empty();
            $.get($(this).attr('href'), function (data) {
            	$("#modal-add-solicitante .modal-body").html(data);
            	$("#modal-add-solicitante").modal();  
            })
            .always(function(){
            	$("#ajax-indicator").hide();
            });
		});

		$('#btn-save-solicitante').click(function(event){
			event.preventDefault();
			var button = $(this);
			var copyButton = button.clone(true);
			var content =  $("#modal-add-solicitante .modal-body");
			var form = content.children('form');
			
			$("#ajax-indicator").show();
			button.addClass('disabled').children('span').text('Salvando...').attr({disabled: 'disabled'});
            $.post(form.attr('action'), form.serialize(), function (response) {
                if ($(response).find('.error').length) {
                	content.html(response);
                	button.replaceWith(copyButton);
                } else {
                    $('#EmprestimoAlunoId').append(new Option($(response).find('#AlunoNome').val(), $(response).find('#AlunoId').val()));
                    $('#EmprestimoAlunoId').select2("val", $(response).find('#AlunoId').val());
                    button.replaceWith(copyButton);
                    $("#modal-add-solicitante").modal('hide');
                }
            })
            .always(function(){
            	$("#ajax-indicator").hide();
            });
		});

		$('#btn-add-projeto').click(function(event){
			event.preventDefault();
			var modal = $("#modal-add-projeto");
			var content =  $("#modal-add-projeto .modal-body");
			
			$("#ajax-indicator").show();
			content.empty();
            $.get($(this).attr('href'), function (data) {
                content.html(data);
                modal.modal();  
            })
            .always(function(){
            	$("#ajax-indicator").hide();
            });
		});

		$('#btn-save-projeto').click(function(event){
			event.preventDefault();
			var button = $(this);
			var copyButton = button.clone(true);
			var content =  $("#modal-add-projeto .modal-body");
			var form = content.children('form');
			
			$("#ajax-indicator").show();
			button.addClass('disabled').children('span').text('Salvando...').attr({disabled: 'disabled'});
            $.post(form.attr('action'), form.serialize(), function (response) {
                if ($(response).find('.error').length) {
                	content.html(response);
                	button.replaceWith(copyButton);
                } else {
                    $('#EmprestimoProjetoId').append(new Option($(response).find('#ProjetoNome').val(), $(response).find('#ProjetoId').val()));
                    $('#EmprestimoProjetoId').select2("val", $(response).find('#ProjetoId').val());
                    button.replaceWith(copyButton);
                    $("#modal-add-projeto").modal('hide');
                }
            })
            .always(function(){
            	$("#ajax-indicator").hide();
            });
		});
		
	});
</script>